﻿Public Class frmTasks

    Public Function Initialize() As Boolean
        Try
            Me.Text = "Tasks"

            Dim listMenu As New List(Of Base.SuperGrid.InfoMenu)
            listMenu.Add(New Base.SuperGrid.InfoMenu(Base.SuperGrid.TipoMenu.MODIFICA, 0))
            listMenu.Add(New Base.SuperGrid.InfoMenu(Base.SuperGrid.TipoMenu.SEPARATOR, 0))
            listMenu.Add(New Base.SuperGrid.InfoMenu(Base.SuperGrid.TipoMenu.ESPORTA, 0))
            listMenu.Add(New Base.SuperGrid.InfoMenu(Base.SuperGrid.TipoMenu.SWITCH_MODIFICA, 0))
            listMenu.Add(New Base.SuperGrid.InfoMenu(Base.SuperGrid.TipoMenu.SEPARATOR, 0))
            listMenu.Add(New Base.SuperGrid.InfoMenu(Base.SuperGrid.TipoMenu.NUMERO_ELEMENTI, 1))
            grdTasks.AddMenu(listMenu)
            listMenu = Nothing
            grdTasks.NomeForm = Me.Name

            cboSprints.Load("Select * From Sprints", "Name", gConnectionStringAgenda.ToString)

            AddHandler cboSprints.OnInsert, AddressOf InterfaceManager.Sprints_OnInsert
            AddHandler cboSprints.OnModify, AddressOf InterfaceManager.Sprints_OnModify

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "LoadData"

    Private Function LoadData(ByVal inSprintsID As Integer) As Boolean
        Try
            grdTasks.DataSource = Data.TTasksHours.GetDTTasksHours(cboSprints.SelectedValue)
            grdTasks.Columns("ID").Visible = False

            Return True
        Catch ex As Exception
            Base.ShowError.Show("LoadData", Me.Name, "LoadData", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub

    Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Try
            Dim ofrmTasksModify As New frmTasksModify
            If ofrmTasksModify.Initialize(0) Then
                If ofrmTasksModify.ShowDialog = vbOK Then
                    Call LoadData(cboSprints.SelectedValue)
                    grdTasks.SelezionaRow(ofrmTasksModify.mID)
                End If
            End If
            ofrmTasksModify = Nothing

        Catch ex As Exception
            Base.ShowError.Show("Insert", Me.Name, "cmdInsert_Click", ex)
        End Try
    End Sub

    Private Sub Modify() Handles grdTasks.OnModifica
        Try
            If grdTasks.CurrentRow IsNot Nothing Then
                Dim intID As Integer = Utility.ConvStrNum(grdTasks.CurrentRow.Cells("ID").Value)

                Dim ofrmTasksModify As New frmTasksModify
                If ofrmTasksModify.Initialize(intID) Then
                    If ofrmTasksModify.ShowDialog = vbOK Then
                        Call LoadData(cboSprints.SelectedValue)
                        grdTasks.SelezionaRow(ofrmTasksModify.mID)
                    End If
                End If
                ofrmTasksModify = Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "Modify", ex)
        End Try
    End Sub

    Private Sub SaveModification(ByRef drRow As DataRow, ByVal strColName As String, ByVal inValueModified As String, ByVal inIDModified As Integer, ByVal inFromRipristina As Boolean)
        Try
            Dim intTasksID As Integer = Utility.ConvStrNum(drRow("ID").ToString)

            If strColName.ToUpper = "1" Or
                strColName.ToUpper = "2" Or
                strColName.ToUpper = "3" Or
                strColName.ToUpper = "4" Or
                strColName.ToUpper = "5" Or
                strColName.ToUpper = "6" Or
                strColName.ToUpper = "7" Or
                strColName.ToUpper = "8" Or
                strColName.ToUpper = "9" Or
                strColName.ToUpper = "10" Or
                strColName.ToUpper = "11" Or
                strColName.ToUpper = "12" Then

            Else

                Dim oTTasks As New Data.TTasks
                If oTTasks.LoadData(intTasksID) Then
                    Select Case strColName.ToUpper
                        Case "Task".ToUpper
                            drRow("Task") = inValueModified
                            oTTasks.mDescription = inValueModified
                        Case "Activity".ToUpper
                            drRow("Activity") = inValueModified
                            oTTasks.mActivitiesID = inIDModified
                        Case "Status".ToUpper
                            drRow("Status") = inValueModified
                            oTTasks.mStatus = inIDModified
                        Case "Exp Hours".ToUpper
                            drRow("Exp Hours") = inValueModified
                            oTTasks.mExpectedHours = inValueModified

                    End Select
                End If

                If Not oTTasks.Modify Then
                    MsgBox("Impossible modify data.", vbExclamation, Me.Text)
                End If
            
                oTTasks = Nothing
            End If

        Catch ex As Exception
            Base.ShowError.Show("Save modification", Me.Name, "SaveModification", ex)
        End Try
    End Sub

#End Region

#Region "Form events"

    Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

#Region "Grid events"

    Private Sub grdTasks_OnModificaInGrid(ByVal intRow As Integer, ByVal intCol As Integer, ByRef outProsegui As Boolean, ByRef outInfoInput As Base.SuperGrid.InfoInput) Handles grdTasks.OnModificaInGrid
        Try
            Select Case grdTasks.Columns(intCol).Name.ToUpper
                Case "US".ToUpper
                    ' nothing
                    Return
                Case "Task".ToUpper
                    outInfoInput.intTipo = Base.SuperGrid.TipoInput.TESTO
                    outInfoInput.intMaxLength = 255
                Case "Activity".ToUpper
                    outInfoInput.intTipo = Base.SuperGrid.TipoInput.COMBO
                    outInfoInput.strQuery = "Select * From Activities"
                    outInfoInput.strNomeCampo = "Name"
                    outInfoInput.strConnectionString = gConnectionStringAgenda.ToString
                Case "Status".ToUpper
                    outInfoInput.intTipo = Base.SuperGrid.TipoInput.LISTA
                    outInfoInput.listValori.Clear()
                    outInfoInput.listValori.Add(TASK_STATUS_PLANNED)
                    outInfoInput.listValori.Add(TASK_STATUS_ONGOING)
                    outInfoInput.listValori.Add(TASK_STATUS_ONHOLD)
                    outInfoInput.listValori.Add(TASK_STATUS_DONE)
                    outInfoInput.listValori.Add(TASK_STATUS_CANCELLED)
                    outInfoInput.listValori.Add(TASK_STATUS_NEXT)
                Case "Exp Hours".ToUpper
                    outInfoInput.intTipo = Base.SuperGrid.TipoInput.TESTO
                    outInfoInput.blnIsDecimal = True
                    outInfoInput.blnOnlyNumber = True
                Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
                    outInfoInput.intTipo = Base.SuperGrid.TipoInput.TESTO
                    outInfoInput.blnIsDecimal = True
                    outInfoInput.blnOnlyNumber = True
                Case Else
                    Return
            End Select
            outProsegui = True
        Catch ex As Exception
            Base.ShowError.Show("Modify data", Me.Name, "grdTasks_OnModificaInGrid", ex)
        End Try
    End Sub

    Private Sub grdTasks_OnModificatoOnGrid(ByVal intRow As Integer, ByVal intCol As Integer, ByVal strValoreModificato As String, ByVal intIDModificato As Integer) Handles grdTasks.OnModificatoOnGrid
        Try
            Dim drRow As DataRow = CType(grdTasks.Rows(intRow).DataBoundItem, DataRowView).Row
            Call SaveModification(drRow, grdTasks.Columns(intCol).Name, strValoreModificato, intIDModificato, False)
        Catch ex As Exception
            Base.ShowError.Show("Modify data", Me.Name, "grdTasks_OnModificatoOnGrid", ex)
        End Try
    End Sub

#End Region

    Private Sub cboSprints_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSprints.SelectedIndexChanged
        Try
            If cboSprints.IsItemSelected Then
                Call LoadData(cboSprints.SelectedValue)
            End If
        Catch ex As Exception
            Base.ShowError.Show("Select sprint", Me.Name, "cboSprints_SelectedIndexChanged", ex)
        End Try
    End Sub
End Class